/**
 * @file engineer_mode_main.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_ENGINEER_MODE!= 0

/*********************
 *      DEFINES
 *********************/
#define ENGINEER_MODE_MAIN_LIST_CNT    7

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void telephony_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void connectivity_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void hardware_testing_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void location_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void audio_calibration_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void pin_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
static void keep_screen_on_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
#if 0
static void firmware_version_create_btn_event_cb(lv_obj_t * btn, lv_event_t e);
#endif
static void init_style(void);
static void engineer_mode_main_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/
const struct {
    watchLangTxtId_t txtId;
    lv_event_cb_t event_cb;
} engineer_mode_main_list[] = {
    { WATCH_TEXT_ID_ENGINEER_MODE_TELEPHONY, telephony_create_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_CONNECTIVITY, connectivity_create_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_HARDWARETESTING, hardware_testing_create_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_LOCATION, location_create_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_AUDIO_CALIBRATION, audio_calibration_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_PIN, pin_create_btn_event_cb },
    { WATCH_TEXT_ID_ENGINEER_MODE_KEEP_SCREEN_ON, keep_screen_on_create_btn_event_cb },
#if 0
    { WATCH_TEXT_ID_ENGINEER_MODE_FIRMWARE_VERSION, firmware_version_create_btn_event_cb },
#endif
};

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
lv_obj_t * engineer_mode_main_create(lv_obj_t * activity_obj)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_MAIN;
        activity_ext.create = engineer_mode_main_create;
        activity_ext.prepare_destory = engineer_mode_main_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    /*Allocate the object type specific extended data*/
    lv_engineer_main_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_engineer_main_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    init_style();

    lv_obj_t * title = lv_label_create(obj, NULL);
    lv_obj_set_size(title, LV_HOR_RES, LV_VER_RES / 4);
    lv_label_set_text_id(title, WATCH_TEXT_ID_ENGINEER_MODE);
    lv_obj_add_style(title, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_obj_align(title, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);

    lv_obj_t * list = lv_list_create(obj, NULL);
    lv_obj_set_size(list, LV_HOR_RES, LV_VER_RES - lv_obj_get_height(title) - 30);
    lv_page_set_edge_flash(list, true);
    lv_obj_add_style(list, LV_LIST_PART_EDGE_FLASH, &lv_watch_cont_opa1);
    lv_obj_align(list, title, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);

    lv_obj_add_style(list, LV_LIST_PART_BG,  &lv_watch_style_transp);

    uint8_t i = 0;
    lv_obj_t * btn;
    lv_obj_t * label;
    for(i = 0; i < ENGINEER_MODE_MAIN_LIST_CNT; i++) {
        btn = lv_list_add_btn(list, NULL, lv_lang_get_text(engineer_mode_main_list[i].txtId));
        lv_obj_add_style(btn, LV_BTN_PART_MAIN, &(ext->list_btn_st));
        lv_obj_set_event_cb(btn, engineer_mode_main_list[i].event_cb);
        label = lv_obj_get_child(btn, NULL);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
        lv_obj_set_width(label, LV_HOR_RES - 10);
    }

    lv_obj_set_click(list, true);

    return obj;
}

lv_engineer_main_obj_ext_t * engineer_mode_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_ENGINEER_MODE_MAIN);
    if(activity_obj == NULL) return NULL;

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) return NULL;

    if(watch_obj) {
        lv_engineer_main_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

lv_obj_t * engineer_mode_item_watch_obj_create(lv_obj_t * activity_obj, lv_watch_activity_prepare_destory_f_t item_prepare_destory)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_ITEM;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = item_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    return obj;
}


lv_obj_t * engineer_mode_item_title_create(lv_obj_t * par, watchLangTxtId_t txtId)
{
    lv_obj_t * label = lv_label_create(par, NULL);
    lv_obj_set_size(label, LV_HOR_RES, LV_VER_RES / 6);
    lv_label_set_text_id(label, txtId);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 5);
    return label;
}

void engineer_mode_item_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

lv_obj_t * cell_info_watch_obj_create(lv_obj_t * activity_obj, lv_watch_activity_prepare_destory_f_t item_prepare_destory)
{
    lv_obj_t * obj;
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_CELL_INFO_ITEM;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = cell_info_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    return obj;
}

lv_cell_info_obj_ext_t * cell_info_get_ext(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_CELL_INFO_ITEM);
    if(activity_obj == NULL) {
        printf("activity_obj is NULL in cell_info_get_ext\n");
        return NULL;
    }

    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    if(watch_obj == NULL) {
        printf("watch_obj is NULL in cell_info_get_ext\n");
        return NULL;
    }

    if(watch_obj) {
        lv_cell_info_obj_ext_t * ext = lv_obj_get_ext_attr(watch_obj);
        return ext;
    }
    return NULL;
}

void cell_info_prepare_destory(lv_obj_t * activity_obj)
{
    lv_cell_info_obj_ext_t * cell_info_ext = cell_info_get_ext();
    LV_ASSERT_MEM(cell_info_ext);

    cell_info_ext->ta = NULL;
    cell_info_ext->cell_info_type = CELL_INFO_INVAILD;
    lv_task_del(cell_info_ext->cell_info_task_p);
    lv_watch_png_cache_all_free();
}

lv_obj_t * hardware_testing_item_watch_obj_create(lv_obj_t * activity_obj, lv_watch_activity_prepare_destory_f_t item_prepare_destory)
{
    lv_obj_t * obj;
    if(NULL == activity_obj) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_ENGINEER_MODE_ITEM_HWT;
        activity_ext.create = NULL;
        activity_ext.prepare_destory = item_prepare_destory;

        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(NULL == activity_obj) {
            return NULL;
        }
    }

    obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(NULL == obj) {
        return NULL;
    }

    return obj;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void telephony_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_telephony_create(NULL);
    }
}

static void connectivity_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_connectivity_create(NULL);
    }
}

static void hardware_testing_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_hardware_testing_create(NULL);
    }
}

static void location_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_location_create(NULL);
    }
}

static void audio_calibration_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_audio_calibration_create(NULL);
    }
}

static void pin_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_pin_create(NULL);
    }
}

static void keep_screen_on_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_keep_screen_on_create(NULL);
    }
}

#if 0
static void firmware_version_create_btn_event_cb(lv_obj_t * btn, lv_event_t e)
{
    if(LV_EVENT_CLICKED == e) {
        engineer_mode_firmware_version_create(NULL);
    }
}
#endif

static void init_style(void)
{
    lv_engineer_main_obj_ext_t * ext = engineer_mode_get_ext();

    lv_style_init(&(ext->list_btn_st));
    lv_style_copy(&(ext->list_btn_st), &lv_watch_style_transp);
    lv_style_set_radius(&(ext->list_btn_st), LV_STATE_DEFAULT, 0);
    lv_style_set_border_width(&(ext->list_btn_st), LV_STATE_DEFAULT, 1);
    lv_style_set_border_color(&(ext->list_btn_st), LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_border_side(&(ext->list_btn_st), LV_STATE_DEFAULT, LV_BORDER_SIDE_BOTTOM);
    lv_style_set_radius(&(ext->list_btn_st), LV_STATE_PRESSED, 0);
    lv_style_set_border_width(&(ext->list_btn_st), LV_STATE_PRESSED, 1);
    lv_style_set_border_color(&(ext->list_btn_st), LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_style_set_border_side(&(ext->list_btn_st), LV_STATE_PRESSED, LV_BORDER_SIDE_BOTTOM);
    lv_style_set_radius(&(ext->list_btn_st), LV_STATE_FOCUSED, 0);
    lv_style_set_border_width(&(ext->list_btn_st), LV_STATE_FOCUSED, 1);
    lv_style_set_border_color(&(ext->list_btn_st), LV_STATE_FOCUSED, LV_COLOR_WHITE);
    lv_style_set_border_side(&(ext->list_btn_st), LV_STATE_FOCUSED, LV_BORDER_SIDE_BOTTOM);

    lv_style_init(&(ext->list_btn_hor_st));
    lv_style_copy(&(ext->list_btn_hor_st), &lv_watch_style_transp);
    lv_style_set_pad_left(&(ext->list_btn_hor_st), LV_STATE_DEFAULT, 10);
    lv_style_set_radius(&(ext->list_btn_hor_st), LV_STATE_DEFAULT, 0);
    lv_style_set_border_width(&(ext->list_btn_hor_st), LV_STATE_DEFAULT, 1);
    lv_style_set_border_color(&(ext->list_btn_hor_st), LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_border_side(&(ext->list_btn_hor_st), LV_STATE_DEFAULT, LV_BORDER_SIDE_BOTTOM);
    lv_style_set_radius(&(ext->list_btn_hor_st), LV_STATE_PRESSED, 0);
    lv_style_set_border_width(&(ext->list_btn_hor_st), LV_STATE_PRESSED, 1);
    lv_style_set_border_color(&(ext->list_btn_hor_st), LV_STATE_PRESSED, LV_COLOR_WHITE);
    lv_style_set_border_side(&(ext->list_btn_hor_st), LV_STATE_PRESSED, LV_BORDER_SIDE_BOTTOM);

    lv_style_init(&(ext->list_btn_inner_st));
    lv_style_copy(&(ext->list_btn_inner_st), &lv_watch_style_transp);
    lv_style_set_pad_inner(&(ext->list_btn_inner_st), LV_STATE_DEFAULT, 1);

    lv_style_init(&(ext->white_st));
    lv_style_copy(&(ext->white_st), &lv_style_pretty);
    lv_style_set_text_font(&(ext->white_st), LV_STATE_DEFAULT, LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_style_set_text_color(&(ext->white_st), LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_style_set_radius(&(ext->white_st), LV_STATE_DEFAULT, 0);
    lv_style_set_text_font(&(ext->white_st), LV_STATE_PRESSED, LV_THEME_WATCH_NIGHT_FONT_SMALL);
    lv_style_set_text_color(&(ext->white_st), LV_STATE_PRESSED, LV_COLOR_BLACK);
    lv_style_set_radius(&(ext->white_st), LV_STATE_PRESSED, 0);

    lv_style_init(&(ext->red_st));
    lv_style_copy(&(ext->red_st), &ext->white_st);
    lv_style_set_bg_color(&(ext->red_st), LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_style_set_bg_grad_color(&(ext->red_st), LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_style_set_bg_color(&(ext->red_st), LV_STATE_PRESSED, LV_COLOR_RED);
    lv_style_set_bg_grad_color(&(ext->red_st), LV_STATE_PRESSED, LV_COLOR_RED);

    lv_style_init(&(ext->green_st));
    lv_style_copy(&(ext->green_st), &ext->red_st);
    lv_style_set_bg_color(&(ext->green_st), LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_bg_grad_color(&(ext->green_st), LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_bg_color(&(ext->green_st), LV_STATE_PRESSED, LV_COLOR_GREEN);
    lv_style_set_bg_grad_color(&(ext->green_st), LV_STATE_PRESSED, LV_COLOR_GREEN);

    lv_style_init(&(ext->blue_st));
    lv_style_copy(&(ext->blue_st), &ext->red_st);
    lv_style_set_bg_color(&(ext->blue_st), LV_STATE_DEFAULT, LV_COLOR_BLUE);
    lv_style_set_bg_grad_color(&(ext->blue_st), LV_STATE_DEFAULT, LV_COLOR_BLUE);

    lv_style_init(&(ext->gray_st));
    lv_style_copy(&(ext->gray_st), &(ext->green_st));
    lv_style_set_bg_color(&(ext->gray_st), LV_STATE_DEFAULT, LV_COLOR_MAKE(0x20, 0x20, 0x20));
    lv_style_set_bg_grad_color(&(ext->gray_st), LV_STATE_DEFAULT, LV_COLOR_MAKE(0x20, 0x20, 0x20));
    lv_style_set_text_color(&(ext->gray_st), LV_STATE_DEFAULT, LV_COLOR_GRAY);
}

static void engineer_mode_main_prepare_destory(lv_obj_t * activity_obj)
{
    lv_engineer_main_obj_ext_t * ext = engineer_mode_get_ext();
    lv_style_reset(&(ext->list_btn_st));
    lv_style_reset(&(ext->list_btn_hor_st));
    lv_style_reset(&(ext->list_btn_inner_st));
    lv_style_reset(&(ext->white_st));
    lv_style_reset(&(ext->green_st));
    lv_style_reset(&(ext->red_st));
    lv_style_reset(&(ext->blue_st));
    lv_style_reset(&(ext->gray_st));

    lv_watch_png_cache_all_free();
}

#endif /*USE_LV_WATCH_ENGINEER_MODE*/
